package datastructure.bst;

public class NBST<E extends Comparable<E>> {
    private class Node {
        E e;
        Node left, right;

        public Node(E e) {
            this.e = e;
            this.left = null;
            this.right = null;
        }
    }

    Node root;
    int size;

    public NBST() {
        this.root = null;
        this.size = 0;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void add(E e) {
        root = add(root, e);
    }

    private Node add(Node node, E e) {
        if (node == null) {
            size++;
            return new Node(e);
        }
        if (e.compareTo(node.e) < 0) {
            node.left = add(node.left, e);
        } else if (e.compareTo(node.e) > 0) {
            node.right = add(node.right, e);
        }
        return node;
    }

    public boolean contains(E e) {
        return contains(root, e);
    }

    private boolean contains(Node root, E e) {
        if (root == null) {
            return false;
        }
        if (e.compareTo(root.e) == 0) {
            return true;
        }
        if (e.compareTo(root.e) < 0) {
            return contains(root.left, e);
        } else {
            return contains(root.right, e);
        }
    }

    void preOrder() {
        preOrder(root);
    }

    private void preOrder(Node node) {
        if (node == null) {
            return;
        }
        System.out.println(node.e);
        preOrder(node.left);
        preOrder(node.right);
    }

    public void tianjia(E e) {
        root = tianjia(root, e);
    }

    private Node tianjia(Node node, E e) {
        if (node == null) {
            size++;
            return new Node(e);
        }
        if (e.compareTo(node.e) < 0) {
            node.left = tianjia(node.left, e);
        } else if (e.compareTo(node.e) > 0) {
            node.right = tianjia(node.right, e);
        }
        return node;
    }

    public void delete(E e) {
        if (root == null) {
            throw new IllegalArgumentException("as");
        }
        root = delete(root, e);
    }

    private Node delete(Node node, E e) {
        if (e.compareTo(node.e) < 0) {
            node.left = delete(node.left, e);
            return node;
        } else if (e.compareTo(node.e) > 0) {
            node.right = delete(node.right, e);
            return node;
        } else {
            if (node.right == null) {
                Node left = node.left;
                node.left = null;
                size--;
                return left;
            }
            if (node.left == null) {
                Node right = node.right;
                node.right = null;
                size--;
                return right;
            }
            Node successor = minmum(node.right);
            successor.right = removeMin(node.right);
            successor.left = node.left;
            node.left = node.right = null;
            return successor;
        }
    }

    // 删除二分搜索树中最小的结点
    public E removeMin() {
        E ret = minmum();
        root = removeMin(root);
        return ret;
    }

    private Node removeMin(Node node) {
        if (node.left == null) {
            Node right = node.right;
            node.right = null;
            size--;
            return right;
        }
        node.left = removeMin(node.left);
        return node;
    }

    // 删除二分搜索树中最大的结点
    public E removeMax() {
        E ret = minmum();
        root = removeMax(root);
        return ret;
    }

    private Node removeMax(Node node) {
        if (node.right == null) {
            Node left = node.left;
            node.left = null;
            size--;
            return left;
        }
        node.right = removeMin(node.right);
        return node;
    }

    // find the min Node
    public E minmum() {
        if (size == 0) {
            return null;
        }
        return minmum(root).e;
    }

    private Node minmum(Node node) {
        if (node.left == null) {
            return node;
        }
        return minmum(node.left);
    }

    // 删除任意结点
    public void delAny(E e) {
        root = delAny(root, e);
    }

    private Node delAny(Node node, E e) {
        if (root == null) {
            return null;
        }
        if (e.compareTo(node.e) < 0) {
            node.left = delAny(node.left, e);
            return node;
        } else if (e.compareTo(node.e) > 0) {
            node.right = delAny(node.right, e);
            return node;
        } else {
            if (node.left == null) {
                Node right = node.right;
                node.right = null;
                size--;
                return right;
            } else if (node.right == null) {
                Node left = node.left;
                node.left = null;
                size--;
                return left;
            } else {
                Node successor = minmum(node.right);
                successor.right = removeMin(node.right);
                successor.left = node.left;
                node.right = node.left = null;
                return successor;
            }
        }
    }
}
